//
//  CustomPageControl.m
//  longbaobao
//
//  Created by Steven.Yung on 16/9/13.
//  Copyright © 2016年 Bylongma. All rights reserved.
//

#import "CustomPageControl.h"

@interface CustomPageControl () {
    UIImageView *activeImageView;
    UIImageView *inactiveImageView;
}

@end

@implementation CustomPageControl

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    
    self = [super initWithCoder:aDecoder];
    
    if (self) {

    }
    
    return self;
}

//- (void)setActiveImage:(UIImage *)activeImage {
//    _activeImage = activeImage;
//    
//    [self setValue:activeImage forKey:@"_currentPageImage"];
//}
//
//- (void)setInactiveImage:(UIImage *)inactiveImage {
//    _inactiveImage = inactiveImage;
//    [self setValue:inactiveImage forKey:@"_pageImage"];
//}

- (void)setCurrentPage:(NSInteger)currentPage {
    [super setCurrentPage:currentPage];
    [self updateDots];
}

- (void)updateDots {
    
    for (NSInteger i = 0; i < [self.subviews count]; i++) {
        
        UIView *dot = [self.subviews objectAtIndex:i];
        
        UIImageView *imageView;
        
        if (i == self.currentPage) {
            imageView = [[UIImageView alloc] initWithImage:_activeImage];
            
        } else {
            imageView = [[UIImageView alloc] initWithImage:_inactiveImage];
        }
        
        CGFloat dotWidth = CGRectGetWidth(dot.bounds);
        CGFloat dotHeight = CGRectGetHeight(dot.bounds);
        CGFloat imageWidth = CGRectGetWidth(imageView.bounds);
        CGFloat imageHeight = CGRectGetHeight(imageView.bounds);
        //如果dot的大小小于图片的大小，将dot的大小改成图片的大小
        if (dotWidth < imageWidth || dotHeight < imageHeight) {
            dot.frame = CGRectMake(dot.frame.origin.x, dot.frame.origin.y, imageWidth, imageHeight);
        }
        //设置imageView的位置，使其在dot中居中
        imageView.frame = CGRectMake((dotWidth - imageWidth)/2, (dotHeight - imageHeight)/2, imageWidth, imageHeight);
        //清除上一次添加的imageView
        for (UIImageView *view in dot.subviews) {
            [view removeFromSuperview];
        }

        [dot addSubview:imageView];
        dot.backgroundColor = [UIColor clearColor];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
